page.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use client";
  2. import { useEffect, useState } from "react";
  3. import { useRouter } from "next/navigation";
  4. import { useI18n } from "locales/client";
  5. import { useWorkoutSessionService } from "@/shared/lib/workout-session/use-workout-session.service";
  6. import { WorkoutSessionList } from "@/features/workout-session/ui/workout-session-list";
  7. import { WorkoutSessionHeatmap } from "@/features/workout-session/ui/workout-session-heatmap";
  8. import { useSession } from "@/features/auth/lib/auth-client";
  9. import { LocalAlert } from "@/components/ui/local-alert";
  10. import { Button } from "@/components/ui/button";
  11. import type { WorkoutSession } from "@/shared/lib/workout-session/types/workout-session";
  12. export default function ProfilePage() {
  13. const router = useRouter();
  14. const t = useI18n();
  15. const [sessions, setSessions] = useState<WorkoutSession[]>([]);
  16. const { getAll } = useWorkoutSessionService();
  17. const { data: session } = useSession();
  18. useEffect(() => {
  19. const loadSessions = async () => {
  20. const loadedSessions = await getAll();
  21. setSessions(loadedSessions);
  22. };
  23. loadSessions();
  24. }, []);
  25. const values: Record<string, number> = {};
  26. sessions.forEach((session) => {
  27. const date = session.startedAt.slice(0, 10);
  28. values[date] = (values[date] || 0) + 1;
  29. });
  30. const until =
  31. sessions.length > 0
  32. ? sessions.reduce((max, s) => (s.startedAt > max ? s.startedAt : max), sessions[0].startedAt).slice(0, 10)
  33. : new Date().toISOString().slice(0, 10);
  34. return (
  35. <div>
  36. {!session && <LocalAlert className="my-4" />}
  37. <WorkoutSessionHeatmap until={until} values={values} />
  38. <WorkoutSessionList />
  39. <div className="mt-8 flex justify-center">
  40. <Button onClick={() => router.push("/")} size="large">
  41. {t("profile.new_workout")}
  42. </Button>
  43. </div>
  44. </div>
  45. );
  46. }